﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;

public partial class KSLDeals_KSLCustomerLookup : System.Web.UI.Page
{

    SqlConnection SQLConn = new SqlConnection();
    System.Data.DataTable dtList = new System.Data.DataTable("List");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["ani"] != null)
                Session["ani"] = Request.QueryString["ani"];
            else
                Session["ani"] = "8013931635";


            if (Request.QueryString["employeeid"] != null)
                Session["employeeid"] = Request.QueryString["employeeid"];
            else
                Session["employeeid"] = "3";


            if (Request.QueryString["incontactid"] != null)
                Session["incontactid"] = Request.QueryString["incontactid"];
            else
                Session["incontactid"] = "0";

            FindCustomer();
        }
        else
        {

        }
    }

    public void FindCustomer()
    {
        System.Data.DataTable temp = SearchCustomer(Session["ani"].ToString());

        if (temp.Rows.Count > 0)
        {
            tbSearch.Text = Session["ani"].ToString();

            ddlSearchResults.DataSource = temp;
            ddlSearchResults.DataTextField = temp.Columns[0].ToString();
            ddlSearchResults.DataValueField = temp.Columns[1].ToString();
            ddlSearchResults.DataBind();

            if (temp.Rows.Count > 1)
                ddlSearchResults.Items.Insert(0, new ListItem("Please select a Business from the List", "0"));

            ddlSearchResults.SelectedIndex = 0;
        }
    }

    public System.Data.DataTable SearchCustomer(string search)
    {
        SQLConn.ConnectionString = "Data Source=172.25.10.8; Initial Catalog=VoiceNet_KSLDeals; User ID = portaluser; Password = sqluser";

        SQLConn.Open();
        if (SQLConn.State == System.Data.ConnectionState.Open)
        {
            //Fill dtList Table
            string SQLList = "Select areacode + number + ' - ' + businessname as textfield, enterpriselistid from list (nolock) where areacode + number like @search or businessname like @search ";
            SqlCommand SqlCmdList = new SqlCommand(SQLList, SQLConn);
            SqlCmdList.Parameters.AddWithValue("@search", "%" + search + "%");
            SqlCmdList.CommandType = System.Data.CommandType.Text;
            SqlDataAdapter dataAdapterList = new SqlDataAdapter(SqlCmdList);
            dataAdapterList.Fill(dtList);

            SQLConn.Close();
        }

        return dtList;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ddlSearchResults.Items.Clear();

        System.Data.DataTable temp = SearchCustomer(tbSearch.Text);

        if (tbSearch.Text.Length < 2)
        {
            ddlSearchResults.Items.Insert(0, new ListItem("Search with at least 2 or more characters", "0"));
        }
        else if (temp.Rows.Count > 0)
        {
            ddlSearchResults.DataSource = temp;
            ddlSearchResults.DataTextField = temp.Columns[0].ToString();
            ddlSearchResults.DataValueField = temp.Columns[1].ToString();
            ddlSearchResults.DataBind();

            if (temp.Rows.Count > 1)
                ddlSearchResults.Items.Insert(0, new ListItem("Please select a Business from the List", "0"));

            ddlSearchResults.SelectedIndex = 0;
        }
        else
        {
            ddlSearchResults.Items.Insert(0, new ListItem("Business Not Found", "0"));
        }
    }

    protected void btnLoadSelected_Click(object sender, EventArgs e)
    {
        if (ddlSearchResults.Items.Count > 0 && ddlSearchResults.SelectedValue != "0")
        {
            InsertCallData(ddlSearchResults.SelectedValue.Trim(), Session["incontactid"].ToString(), Session["employeeid"].ToString());
            Response.Redirect("KSLDeals.aspx?listid=" + ddlSearchResults.SelectedValue.Trim() + "&servername=COGSQL&projectname=Voicenet_KSLDeals&campaignname=KSLDeals&agentid=" + Session["employeeid"].ToString());
        }
    }

    protected void InsertCallData(string callid, string incontactid, string agentid)
    {
        SQLConn.ConnectionString = "Data Source=172.25.10.8; Initial Catalog=VoiceNet_KSLDeals; User ID = portaluser; Password = sqluser";

        SQLConn.Open();
        if (SQLConn.State == System.Data.ConnectionState.Open)
        {
            string SQLList = "exec f_InsertCallData " + callid + ", " + incontactid + ", " + agentid;
            SqlCommand SqlCmdList = new SqlCommand(SQLList, SQLConn);
            SqlCmdList.CommandType = System.Data.CommandType.Text;
            SqlCmdList.ExecuteNonQuery();
            SQLConn.Close();
        }
    }
}